﻿
namespace ManagerClinic.Ultilities.Ultility
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.IO;  

    public static class LogExceptionUltilities
    {
        public static void LogException(string function, string message)
        {
            StringBuilder sb = new StringBuilder();
            try
            {
                // Get the current directory.
                string path = Directory.GetCurrentDirectory();
                string target = path + "\\Log\\" + DateTime.Now.Day + DateTime.Now.Month + DateTime.Now.Year + ".txt";

                if (!File.Exists(target))
                {
                    sb.AppendLine();
                    sb.AppendLine("====================================" + function + "=======================================");
                    sb.AppendLine(function);
                    sb.AppendLine(message);
                    sb.AppendLine("===========================================================================================");
                }
                else
                {
                    using (StreamReader sr = new StreamReader(target))
                    {
                        String line;
                        // Read and display lines from the file until the end of
                        // the file is reached.
                        while ((line = sr.ReadLine()) != null) { sb.AppendLine(line); }
                    }
                    
                    sb.AppendLine();
                    sb.AppendLine("====================================" + function + "=======================================");
                    sb.AppendLine(function);
                    sb.AppendLine(message);
                    sb.AppendLine("===========================================================================================");
                }

                using (StreamWriter sw = new StreamWriter(target)) { sw.Write(sb.ToString()); }
            }
            catch (Exception e) { Console.WriteLine("The process failed: {0}", e.ToString()); }
            finally { sb = null; }
        }
    }
}
